<template>
  <a-drawer
      :width="600"
      placement="right"
      :visible="visible"
      :after-visible-change="afterVisibleChange"
      @close="onClose"
      title="版本管理"
      class="form-drawer"
  >
    <a-form-model
        :model="menuForm"
        ref="menuForm"
        :rules="rules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        class="form-model"
    >
      <a-form-model-item label="版本号：" prop="newversion">
        <a-input v-model="menuForm.newversion" placeholder="请输入" allow-clear></a-input>
      </a-form-model-item>
      <a-form-model-item label="更新内容：" prop="content">
        <a-input v-model="menuForm.content" placeholder="请输入" allow-clear></a-input>
      </a-form-model-item>
      <a-form-model-item label="资源包" prop="downloadurl">
        <upload-file v-model="menuForm.downloadurl"  uploadMode="manual" />
      </a-form-model-item>
      <a-form-model-item :wrapper-col="{ span: 18, offset: 3 }">
        <a-button type="primary" @click="menuSubmit" :loading="submitLoading">保存</a-button>
        <a-button @click="onClose">取消</a-button>
      </a-form-model-item>
    </a-form-model>
  </a-drawer>
</template>

<script>
import { addAppVersion } from '@/api/short'
import UploadImage from '@/components/UploadImage'
import UploadFile from "@/components/UploadFile/UploadFile.vue";
export default {
  name: 'AppVersionForm',
  data () {
    return {
      visible: false,
      menuForm: {
        hiddenValue: true,
        downloadurl: '',
        newversion: ''
      },
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      rules: {
        newversion: [{ required: true, message: '版本号不能为空' }],
        content: [{ required: true, message: '更新内容不能为空' }],
        downloadurl: [{ required: true, message: '请上传资源包' }],
        level: [{ required: true, message: '菜单级别不能为空' }],
        path: [{ required: true, message: '菜单URL不能为空' }]
      },
      parentList: [],
      submitLoading: false
    }
  },
  methods: {
    open (record = {}) {
      if (record.id) {
        this.getMenuInfo(record.id)
      }
      this.visible = true
    },
    onClose () {
      this.visible = false
    },
    afterVisibleChange (val) {
      // console.log('visible', val)
      // if (!val) {
      //   this.menuForm = {
      //     menuCode: '',
      //     menuName: '',
      //     path: '',
      //     orderNum: '0',
      //     parentId: '',
      //     hiddenValue: true,
      //     icon: ''
      //   }
      // }
      // this.getParentList()
    },
    getParentList () {
      getAllMenuList({}).then((res) => {
        console.log(res)
        this.parentList = res.data
      })
    },
    filterOption (input, option) {
      return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
      )
    },
    getMenuInfo (id) {
      getMenuInfo(id).then((res) => {
        console.log(res)
        res.data.hiddenValue = res.data.visible == 1
        res.data.parentId = res.data.parentId === 0 ? null : res.data.parentId
        this.menuForm = res.data
      })
    },
    menuSubmit () {
      this.menuForm.visible = this.menuForm.hiddenValue ? '1' : '0'
      this.$refs.menuForm.validate((valid) => {
        if (valid) {
          this.submitLoading = true
          addAppVersion(this.menuForm)
              .then((res) => {
                this.$notification['success']({
                  message: '提示',
                  description: `${this.menuForm.id ? '编辑' : '新增'}成功`
                })
                this.onClose()
                this.submitLoading = false
                this.$emit('on-submit')
              })
              .catch(() => {
                this.submitLoading = false
              })
        }
      })
    }
  },
  components: {
    UploadFile,
    UploadImage
  }
}
</script>

<style></style>
